<?php
class product extends CI_Controller
{
    protected $_table = "mock_products";

    public function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('product_model');
        $this->load->model('brand_model');
        $this->load->model('category_model');
        $this->load->helper(array('form', 'url'));
        $this->load->helper("url");
        $this->load->library('upload');
    }


    public function index()
    {
        $data['title'] = "List Product";
        $data['path'] = "product/list";

        $config['full_tag_open'] = '<ul class="tsc_pagination tsc_paginationA tsc_paginationA01">';
        $config['full_tag_close'] = '</ul>';
        $config['prev_link'] = '&lt;';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&gt;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="current"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';

        $config['first_link'] = '&lt;&lt;';
        $config['last_link'] = '&gt;&gt;';
        $config['base_url'] = base_url().'admincp/product/index'; //url page cần PT
        $config['total_rows'] = $this->product_model->count_all(); //Total record
        $config['uri_segment'] = 4;
        $config['per_page'] = 3;  // Số record hiển thị trên 1 trang
        $this->pagination->initialize($config);
        $start = $this->uri->segment(4);
        $data['list_all'] = $this->product_model->list_all($config['per_page'],$start);
        $data['listbrand'] = $this->brand_model->listBran_product();
        $data['listcate'] = $this->category_model->listCate_product();
        $this->load->view("dashboard/index",$data);
    }
    public function addProduct(){
        $data['path'] = "product/add";
        $data['title'] = "Add Product";
        $data['errors'] = "";
        $data['listbrand'] = $this->brand_model->listBran_product();
        $data['listcate'] = $this->category_model->listCate_product();
        if($this->input->post('submit') != NULL){
            $this->form_validation->set_rules('name', 'Product Name', 'required');
            if ($this->form_validation->run() == FALSE) {
                $this->load->view("dashboard/index", $data);
            }else{
                $this->form_validation->set_rules('price', 'Price', 'required|numeric');
                if ($this->form_validation->run() == FALSE) {
                    $this->load->view("dashboard/index", $data);
                }else{
                    $this->form_validation->set_rules('sale','Sale','numeric');
                    if ($this->form_validation->run() == FALSE) {
                        $this->load->view("dashboard/index", $data);
                    }else{
                        $config['upload_path'] = './images/';
                        $config['allowed_types'] = 'gif|jpg|png';
                        $config['encrypt_name'] = TRUE;
                        foreach ($_FILES['images']["name"] as $key => $value) {
                            $_FILES['picture']['name'] = $_FILES['images']['name'][$key];
                            $_FILES['picture']['type'] = $_FILES['images']['type'][$key];
                            $_FILES['picture']['tmp_name'] = $_FILES['images']['tmp_name'][$key];
                            $_FILES['picture']['error'] = $_FILES['images']['error'][$key];
                            $_FILES['picture']['size'] = $_FILES['images']['size'][$key];

                            $this->load->library('upload', $config);
                            $this->upload->initialize($config);
                            if (!$this->upload->do_upload('picture')) {

                                $data['errors'] = array('error' => $this->upload->display_errors());

                                $this->load->view('drashboard/index', $data);
                            } else {
                                $data_name = $this->upload->data();
                                $name_image[] = $data_name['file_name'];
                            }
                        }
                        $newimg = serialize($name_image);
                        //echo $newimg; die();
                        foreach($this->input->post('category') as $value){
                            $categories[] = $value;
                        }
                        $sale = $this->input->post("sale");
                        $name = $this->input->post("name");
                        $price = $this->input->post("price");
                        $description = $this->input->post("description");
                        $category = implode(',',$categories);
                        $brand = $this->input->post("brand");
                        $new = $this->input->post("new");


                        $dataarray = array(
                            'product_sale' => $sale,
                            'product_name' => $name,
                            'product_price' => $price,
                            'product_image' => $newimg,
                            'product_description' => $description,
                            'cate_id' => $category,
                            'brand_id' => $brand,
                            'product_new' => $new
                        );
                        $checkName = $this->product_model->checkName($name);
                        if($checkName == FALSE){
                            $data['errors'] = "Tên sản phẩm đã tồn tại";
                            $this->load->view("dashboard/index", $data);
                        }else{
                            $this->product_model->addProduct($dataarray);
                            redirect(base_url() . "admincp/product");
                            exit();
                        }
                    }
                }
            }
        }else{
            $this->load->view('dashboard/index',$data);

        }
    }
    public function detailProduct(){
        $id = $this->uri->segment(4);
        $data['path'] = 'product/detail';
        $data['title'] = "Chi tiết";
        $data['info'] = $this->product_model->getdata($id);
        $data['listbrand'] = $this->brand_model->listBran_product();
        $data['listcate'] = $this->category_model->listCate_product();
        $this->load->view("dashboard/index",$data);
    }
    public function deleteProduct(){
        $id = $this->uri->segment(4);
        $this->product_model->deleteProduct($id);
        redirect(base_url().'admincp/product');
        exit();
    }
    public function editProduct(){
        $id = $this->uri->segment(4);
        $data['path'] = 'product/edit';
        $data['title'] = 'Edit Product';
        $data['errors'] = "";
        $data['info'] = $this->product_model->getdata($id);
        $data['listbrand'] = $this->brand_model->listBran_product();
        $data['listcate'] = $this->category_model->listCate_product();
        if($this->input->post('submit') != NULL){
            $this->form_validation->set_rules('name', 'Product Name', 'required');
            if ($this->form_validation->run() == FALSE) {
                $this->load->view("dashboard/index", $data);
            }else{
                $this->form_validation->set_rules('price', 'Price', 'required|numeric');
                if ($this->form_validation->run() == FALSE) {
                    $this->load->view("dashboard/index", $data);
                }else{
                    $this->form_validation->set_rules('sale','Sale','numeric');
                    if ($this->form_validation->run() == FALSE) {
                        $this->load->view("dashboard/index", $data);
                    }else{
                        $config['upload_path'] = './images/';
                        $config['allowed_types'] = 'gif|jpg|png';
                        $config['encrypt_name'] = TRUE;
                        foreach ($_FILES['images']["name"] as $key => $value) {
                            $_FILES['picture']['name'] = $_FILES['images']['name'][$key];
                            $_FILES['picture']['type'] = $_FILES['images']['type'][$key];
                            $_FILES['picture']['tmp_name'] = $_FILES['images']['tmp_name'][$key];
                            $_FILES['picture']['error'] = $_FILES['images']['error'][$key];
                            $_FILES['picture']['size'] = $_FILES['images']['size'][$key];

                            $this->load->library('upload', $config);
                            $this->upload->initialize($config);
                            if (!$this->upload->do_upload('picture')) {
                                echo "a";
                                die();
                                //$data['error'] = array('error' => $this->upload->display_errors());

                                //$this->load->view('index', $data);
                            } else {
                                $data_name = $this->upload->data();
                                $name_image[] = $data_name['file_name'];
                            }
                        }
                        $newimg = serialize($name_image);
                        //echo $newimg; die();
                        foreach($this->input->post('category') as $value){
                            $categories[] = $value;
                        }
                        $sale = $this->input->post("sale");
                        $name = $this->input->post("name");
                        $price = $this->input->post("price");
                        $description = $this->input->post("description");
                        $category = implode(',',$categories);
                        $brand = $this->input->post("brand");
                        $new = $this->input->post("new");


                        $dataarray = array(
                            'product_sale' => $sale,
                            'product_name' => $name,
                            'product_price' => $price,
                            'product_image' => $newimg,
                            'product_description' => $description,
                            'cate_id' => $category,
                            'brand_id' => $brand,
                            'product_new' => $new
                        );
                        $checkName = $this->product_model->checkName($name);
                        if($checkName == FALSE){
                            $data['errors'] = "Tên sản phẩm đã tồn tại";
                            $this->load->view("dashboard/index", $data);
                        }else{
                            $this->product_model->addProduct($dataarray);
                            redirect(base_url() . "admincp/product");
                            exit();
                        }
                    }
                }
            }
        }else{
            $this->load->view('dashboard/index',$data);
        }
    }
}